$(function () {
    var pblogNickName=Core.getCookie("tz-cms-username");
    var pblogQQ=Core.getCookie("tz-cms-qq");
    var pblogEmail=Core.getCookie("tz-cms-email");
    if(pblogNickName!=""){
        $("#user-name-content").show();
        $("#user-name").text(pblogNickName);
        $("#nickname").val(pblogNickName);
        $("#qq").val(pblogQQ);
        $("#email").val(pblogEmail);
    }else{
        $("#user-info").show();
    }
    $("#qq").blur(function(){
        var qq=$("#qq").val();
        var nickname=$("#nickname").val();
        if(nickname.length==0&&qq.length > 0){
            if(!isNaN(qq)){
                Core.getQqInfo(qq,function (data) {
                    $("#nickname").val(data.nickname);
                });
            }else{
                layer.msg("qq格式不正确！")
            }
        }
    });

    function init(pageNumber) {
        Core.postAjax("/blog/api/comments",{"sid":sid,"pageNumber": (pageNumber==null? 1 : pageNumber), "pageSize": 10},function (data) {
            var commentOne="";
            if(data.total==0){
                commentOne+='<div class="no-comment">暂无评论，快来占领宝座</div>';
                $("#comment-ul").append(commentOne);
            }else{
                $.each(data.records,function (index,value) {
                    commentOne+='<li class="zoomIn article">';
                    commentOne+='				<div class="comment-parent">';
                    commentOne+='					<a id="remark-1"></a>';
                    commentOne+='					<img src="'+(value.avatar||"/assets/img/user-default.png")+'" />';
                    commentOne+='					<div class="info">';
                    commentOne+='						<span class="username">'+value.nickname+'</span>';
                    commentOne+='					</div>';
                    commentOne+='					<div class="comment-content">'+value.content+'</div>';
                    commentOne+='					<p class="info info-footer">';
                    commentOne+='						<i class="fa fa-map-marker" aria-hidden="true"></i>';
                    commentOne+='						<span>未知</span>';
                    commentOne+='						<span class="comment-time">'+value.createTime+'</span>';
                    commentOne+='						<a href="javascript:;" class="reply" data-targetid="1" data-targetname="'+value.nickname+'" pid="'+value.id+'" reply-id="'+value.id+'">回复</a>';
                    commentOne+='					</p>';
                    commentOne+='				</div>';
                    if(value.children!=null){
                        $.each(value.children,function (i,v) {
                            commentOne+='<hr/>';
                            commentOne+='	<div class="comment-child">';
                            commentOne+='					<a id="reply-1"></a>';
                            commentOne+='					<img src="'+(v.avatar||"/assets/img/user-default.png")+'" >';
                            commentOne+='					<div class="info">';
                            commentOne+='						<span class="username">'+v.nickname+'</span>';
                            commentOne+='						<span style="padding-right:0;margin-left:-5px;">回复</span>';
                            commentOne+='						<span class="username">'+v.replyName+'</span>';
                            commentOne+='						<span>'+v.content+'</span>';
                            commentOne+='					</div>';
                            commentOne+='					<p class="info">';
                            commentOne+='						<i class="fa fa-map-marker" aria-hidden="true"></i>';
                            commentOne+='						<span>未知</span>';
                            commentOne+='						<span class="comment-time">'+v.createTime+'</span>';
                            commentOne+='						<a href="javascript:;" class="reply" data-targetid="2" data-targetname="'+v.nickname+'" pid="'+value.id+'" reply-id="'+v.id+'">回复</a>';
                            commentOne+='					</p>';
                            commentOne+='				</div>';
                            commentOne+='               </li>';
                        })
                    }
                })
                $("#comment-more").remove();
                if(data.hasNextPage){
                    commentOne+='<div id="comment-more" data-page="'+data.nextPage+'" class="comment-more">加载更多</div>'
                }
                $("#comment-ul").append(commentOne);
                /*加载更多*/
                $("#comment-more").click(function () {
                    init($(this).attr("data-page"));
                })
                /*link至评论*/
                $(".comment-link").click(function () {
                    var commentLinkId = $(this).attr("data-link");
                    $("html,body").animate({
                        scrollTop:$("#"+commentLinkId).offset().top-55},{duration: 300,easing: "swing"})
                })

                $(".reply").click(function () {
                    var pid=$(this).attr("pid");
                    var replyId=$(this).attr("reply-id");
                    if($("#reply-comment-form").length>0){
                        $replyForm=$("#reply-comment-form");
                        $("#reply-comment-form").remove();
                        $(this).parent().after($replyForm);
                        $("#reply-comment-form").show();
                        $(".reply[style='display: none;']").next().hide();
                        $(".reply[style='display: none;']").show();
                        $("#replyId").val(replyId);
                    }else{
                        var replyForm=' <div class="replycontainer">';
                        replyForm+='                        <form id="reply-comment-form" class="layui-form" >';
                        replyForm+='                            <input type="hidden" name="sid" value="'+sid+'">';
                        replyForm+='                            <input type="hidden" name="pid" value="'+pid+'">';
                        replyForm+='                            <input type="hidden" name="replyId" value="'+replyId+'">';
                        replyForm+='                            <div  class="layui-form-item"  style="display:'+(pblogNickName==""?"block":"none")+'" >';
                        replyForm+='                              <div class="layui-col-xs6 layui-col-sm4 layui-col-md4">';
                        replyForm+='						         <input id="nickname" type="text" class="layui-input"  name="nickname" value="'+pblogNickName+'" placeholder="昵称（必填）">';
                        replyForm+='					           </div>';
                        replyForm+='					          <div class="layui-col-xs6 layui-col-sm4 layui-col-md4">';
                        replyForm+='					        	<input id="qq" type="text" class="layui-input"  name="qq" value="'+pblogQQ+'" placeholder="QQ（用于展示头像和昵称）">';
                        replyForm+='					          </div>';
                        replyForm+='					           <div class="layui-col-xs6 layui-col-sm4 layui-col-md4">';
                        replyForm+='					          	<input id="email" type="text" class="layui-input"  name="email" value="'+pblogEmail+'" placeholder="邮箱">';
                        replyForm+='					           </div>';
                        replyForm+='                             </div>';
                        replyForm+='                            <div class="layui-form-item">';
                        replyForm+='                                <textarea id="reply-content"  name="content"  placeholder="请输入回复内容" class="layui-textarea" style="min-height:80px;"></textarea>';
                        replyForm+='                            </div>';
                        replyForm+='                            <div class="layui-form-item">';
                        replyForm+='                                <button class="layui-btn layui-btn-xs" id="submitReplyCommentBtn" >提交</button>';
                        replyForm+='                            </div>';
                        replyForm+='                        </form>';
                        replyForm+='                    </div>';
                        $(this).parent().parent().after(replyForm);
                        $("#reply-qq").blur(function(){
                            var qq=$("#reply-qq").val();
                            var nickname=$("#reply-nickname").val();
                            if(nickname.length==0&&qq.length > 0){
                                if(!isNaN(qq)){
                                    Core.getQqInfo(qq,function (data) {
                                        $("#reply-nickname").val(data.nickname);
                                    });
                                }else{
                                    layer.msg("qq格式不正确！")
                                }
                            }
                        });
                    }
                    $(this).hide();
                    $(this).next().show();
                    $("#submitReplyCommentBtn").on('click',function (ev) {
                        ev.stopPropagation();
                        ev.preventDefault();
                        if($("#reply-nickname").val()==""){
                            layer.msg("请输入昵称")
                            return;
                        }else if($("#reply-comment-textarea").val()==""){
                            layer.msg("说点什么吧")
                            return;
                        }
                        Core.postAjax("/blog/api/comment/save",$("#reply-comment-form").serialize(),function (data) {
                            if(Core.getCookie("tz-cms-username")==""){
                                Core.setCookie("tz-cms-username",$("#reply-nickname").val(),30);
                                Core.setCookie("tz-cms-qq",$("#reply-qq").val(),30);
                                Core.setCookie("tz-cms-email",$("#reply-email").val(),30);
                            }
                            layer.msg(data.msg, {
                                offset: '30%',
                                time: 800
                            }, function () {
                                if(data.status==200){
                                    location.reload();
                                }
                            });
                        })
                    })
                })
                $(".cancel-reply").click(function () {
                    $("#reply-comment-form").hide();
                    $(this).hide();
                    $(this).prev().show();
                })
                $(".comment-support").click(function () {
                    $thisLove = $(this);
                    Core.postAjax("/blog/api/love",{"bizId":$(this).attr("biz-id"),"bizType":2},function (data) {
                        if(data.status==200){
                            $thisLove.text(parseInt($thisLove.text())+1);
                        }
                    });
                })
            }

        })
    }
    init(1);

    /*提交评论*/
    $("#submitCommentBtn").click(function (ev) {
        ev.stopPropagation();
        ev.preventDefault();
        if($("#nickname").val()==""){
            layer.msg("请输入昵称")
            return;
        }else if($("#content").val()==""){
            layer.msg("说点什么吧")
            return;
        }
        $("#comment-sid").val(sid);
        Core.postAjax("/blog/api/comment/save",$("#comment-form").serialize(),function (data) {
            layer.msg(data.msg, {
                offset: '30%',
                time: 800
            }, function () {
                if(data.status==200){
                    if(pblogNickName!=$("#nickname").val()||pblogQQ!=$("#qq").val()||pblogEmail!=$("#email").val()){
                        Core.setCookie("tz-cms-username",$("#nickname").val(),30);
                        Core.setCookie("tz-cms-qq",$("#qq").val(),30);
                        Core.setCookie("tz-cms-email",$("#email").val(),30);
                    }
                    location.reload();
                }
            });
        })
    })
    /*点击用户名*/
    $("#user-name").click(function () {
        if($("#user-info").hasClass("user-show")){
            $("#user-info").slideUp();
            $("#user-info").removeClass("user-show");
        }else{
            $("#user-info").slideDown();
            $("#user-info").addClass("user-show");
        }

    })
})

